public with sharing class MaintenanceReportController {

	public WorkOrder__c workOrder{get;set;}
	public Account account {get;set;}

	public Id id{get;set;}
	// public List<ManufactureInstallationInfo__c> mii{get;set;}

	public Attachment useSign {get;set;}
	public Attachment manageSign {get;set;}
	public String useSignDate{get;set;}
	public String manageSignDate{get;set;}

	public String equipmentModel{get;set;}

	public Decimal partsCost{get;set;}//零件费
	public Decimal totalCost{get;set;}//总计

	public Attachment engineerSign{get;set;}
	public String engineerSignDate{get;set;}
	public String equipmentName {get;set;}
	public List<String> maintenanceContent {set;get;}//保养内容
	public List<String> maintenanceItem {set;get;}//保养项目
	public List<WorkOrderProduct__c> wpList{get;set;}
	public String engineerName {get;set;}//工程师 经销商工单取 服务工程师名字 否则 技术人员
	public String serStartTime {get;set;}//服务开始时间
	public String serEndTime {get;set;}//服务开始时间



    public MaintenanceReportController(ApexPages.StandardController con) {
        id=con.getId();
        workOrder = [	SELECT 	w.Account__r.Name,w.Account__r.Address__c,w.Account__r.Postcode__c,
        						w.ServiceManufacture__r.Product__r.Model__c,w.Software_Version__c,
        						w.ServiceManufacture__r.Product__r.MaintananceContent__c,
        						w.ServiceManufacture__r.Name,w.ServiceManufacture__r.EndDate__c,
								w.ServiceManufacture__r.Product__r.Name ,
								w.ServiceManufacture__r.Brand__c,
        						w.ServiceManufacture__r.Product__r.SoftwareVersion__c,
        						w.SN__c,w.Maintenance_Content__c,w.Name,w.InstallRequestDate__c,
        						w.IMEINumber__c,w.IMEINumber_F__c,TechExpense__c,TravelExpense__c,
	        					w.UserDepartmen__c,w.UserDepartmentContact__r.Name,w.UseDeptTeleNo__c,
                     			w.MaintenanceItems__c,w.Technician__r.Name,w.Case__r.ServiceEngineerName__c,w.Case__r.RecordType.DeveloperName,
	        					w.ManagementDep__c,w.ManagementDepContact__r.Name,w.ManageDeptTeleNo__c,
	        					w.Install_End_Datetime__c,w.ActuallyWorkTime__c,w.WarrantyStatus__c,
	        					(	SELECT wp.Id,wp.Quantity__c,Product__r.Name ,Product__r.NCCode__c,wp.UnitPrice__c,wp.SalesPrice__c,wp.Name,wp.NewPartsNumber__c
	        						FROM w.WorkOrderuQf3__r wp 
	        						WHERE WorkOrder__c = :id),
	        					w.TransportTime__c,w.Maintenance_Info__c,w.Reason_Desc__c,w.Fault_Freq__c,
	        					w.ProblemType__c,w.Software_Name__c,
	        					w.Install_Start_Datetime__c,
	        					w.RecordType.DeveloperName, 
	        					w.Reporter_Version__c,w.DealerNameTxt__c
        				FROM WorkOrder__c w 
						WHERE w.Id = :id ];
		if(null!=workOrder){

			wpList = workOrder.WorkOrderuQf3__r;

			partsCost = 0;//零件费
			for(WorkOrderProduct__c wp:wpList){
				partsCost+=(wp.SalesPrice__c==null?0:wp.SalesPrice__c);
			}

			totalCost=0;//总费用
			totalCost = partsCost+(workOrder.TechExpense__c==null?0:workOrder.TechExpense__c)+(workOrder.TravelExpense__c==null?0:workOrder.TravelExpense__c);

			//不满4个零件要补齐空行
			Integer atLeastRowsCount = 4;
			if(wpList.size()<atLeastRowsCount){
				Integer wpListSize = wpList.size();
				for(Integer i=0;i<(atLeastRowsCount-wpListSize);i++){
					wpList.add(new WorkOrderProduct__c());
				}
			}

			List<Attachment> attachment= [select Body,Name,CreatedDate,Id from Attachment where ParentId = :id];
			for(Attachment att : attachment){
				if (att.Name=='使用科室负责人_Sign.jpg'){
					useSign=att;
				}
			}

			serStartTime = workOrder.Install_Start_Datetime__c != null ? workOrder.Install_Start_Datetime__c.format('yyyy年MM月dd日'):'';
			serEndTime = workOrder.Install_End_Datetime__c != null ? workOrder.Install_End_Datetime__c.format('yyyy年MM月dd日'):'';
			if(workOrder.Install_End_Datetime__c != null )useSignDate =serEndTime;
			if(workOrder.Install_End_Datetime__c != null )engineerSignDate = serEndTime;
			engineerName = workOrder.Case__r.RecordType.DeveloperName == 'MaintenanceOrder' ? workOrder.Case__r.ServiceEngineerName__c : workOrder.Technician__r.Name;
			equipmentModel = Utils.wrapWordInPdf(workOrder.ServiceManufacture__r.Product__r.Model__c, '\n', 12);
			equipmentName = Utils.wrapWordInPdf(workOrder.ServiceManufacture__r.Brand__c, '\n', 12);
            maintenanceContent =autoNextLine(workOrder.Maintenance_Content__c,47);
            maintenanceItem = autoNextLine(workOrder.MaintenanceItems__c,47);
		}
    }
    private List<String> autoNextLine(String value, Integer posLength ){
        List <String> temp = new List<String>();
      if(value == null) 
      {
          temp.add(value);
          return temp; 
      }
         List<String> str = new List<String>();
          str =  value.split('\r\n',-1);
          for (String s :str){

              if(s.length() > posLength){
                 for(Integer i = 0; i < s.length(); i++){
                 if(Math.mod(i, posLength ) == 0){
                       if(s.length() > i + posLength){
                         temp.add(s.substring(i, i + posLength)); 
                     }else{ 
                         temp.add(s.substring(i,s.length()));
                      } 
                 
                // kaigyoCount[col]++;
                    }
              }
              
              }else{
                  temp.add(s);
              
              }
          }
         
          return temp;
    }

}